package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.text.html.HTMLDocument.HTMLReader.ParagraphAction;

import util.StringUtil;
import model.Employee;

public class EmployeeDao {
public int addEmp(Connection con,Employee employee) throws Exception{
	String sql="insert into employee values (null,?,?,?,?,?,?,?,?)";
	PreparedStatement pstmt=con.prepareStatement(sql);
	pstmt.setString(1,employee.getEmpName());
	pstmt.setString(2,employee.getEmpSex());
	pstmt.setString(3,employee.getEmpBirth());
	pstmt.setString(4,employee.getEmpIdent());
	pstmt.setString(5,employee.getEmpDep());
	pstmt.setString(6,employee.getEmpTel());
	pstmt.setString(7,employee.getEmpDuty());
	pstmt.setString(8,employee.getEmpEdu());
    return pstmt.executeUpdate();	
}
public ResultSet employeelist(Connection con,Employee employee) throws Exception{
	StringBuffer sb=new StringBuffer("select * from employee ");
	if(StringUtil.isNotEmpty(employee.getEmpName())){
		sb.append(" and empName like '%"+employee.getEmpName()+"%'");
	}
	PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
	return pstmt.executeQuery();
}
public int empdelete(Connection con ,String empName) throws Exception{
	String sql="delete from employee where empName=?";
	PreparedStatement pstmt=con.prepareStatement(sql);
	pstmt.setString(1, empName);
	return pstmt.executeUpdate();
}
public int empmodify(Connection con,Employee employee) throws Exception{
	String sql="update employee set empSex=?,empBirth=?,empIdent=?,"
			+ "empDep=?,empTel=?,empDuty=?,empEdu=? where empName=?";
	PreparedStatement pstmt=con.prepareStatement(sql);
	pstmt.setString(1, employee.getEmpSex());
	pstmt.setString(2, employee.getEmpBirth());
	pstmt.setString(3, employee.getEmpIdent());
	pstmt.setString(4, employee.getEmpDep());
	pstmt.setString(5, employee.getEmpTel());
	pstmt.setString(6, employee.getEmpDuty());
	pstmt.setString(7, employee.getEmpEdu());
	pstmt.setString(8, employee.getEmpName());
	return pstmt.executeUpdate();
}
}
